<?php
/**
 * Project: Recipe
 * File: SetDealAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-4-21
 * Time: 下午2:17
 */


class SetDealAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");
		$setdeal = M('SetDeal');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				$total = $setdeal->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $setdeal->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/SetDeal/index');
			}
		}else{
			$total = $setdeal->count();
			$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show = $Page->show();// 分页显示输出
			$list = $setdeal->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 指向添加份量单位界面
	 */
	function add(){
		$this->display();
	}

	/**
	 * 添加份量单位
	 */
	function insert(){
		$setdeal = D('SetDeal');
		if($data = $setdeal->create()){
			if($setdeal->add()){
				$this->success('添加份量单位成功', __APP__.'/SetRecipeInfo/index');
			}else{
				$this->error('添加份量单位失败');
			}
		}else{
			$this->error($setdeal->getError(), __APP__.'/SetDeal/add');
		}
	}

	/**
	 * 指向修改份量单位界面
	 */
	function edit(){
		if(isset($_GET['id']) && !empty($_GET['id'])){
			$setdeal = M('SetDeal');
			$data = $setdeal->find($_GET['id']);
			$this->assign('data', $data);
			$this->display();
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 修改份量单位
	 */
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$setdeal = M('SetDeal');
			if($data = $setdeal->create()){
				if($setdeal->save()){
					$this->success('修改份量单位成功',  __APP__.'/SetRecipeInfo/index');
				}else{
					$this->error('修改份量单位失败');
				}
			}else{
				$this->error($setdeal->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除份量单位
	 */
	function del(){
		if(!empty($_POST['actions']) || !empty($_GET['id'])){
			$setdeal = M('SetDeal');
			$fooddeal = M('FoodDeal');
			if(isset($_GET['id']) && !empty($_GET['id'])){
				$fooddealinfo = $fooddeal->where("unit=".$_GET['id'])->count();
				if($fooddealinfo>0){
					$this->error('此份量单位已被使用,暂不能被删除');
				}else{
					$num = $setdeal->delete($_GET['id']);
				}
			}else{
				foreach($_POST['actions'] as $key=>$value){
					$fooddealinfo = $fooddeal->where("unit=".$value)->count();
					if($fooddealinfo>0){
						$this->error('此份量单位已被使用,暂不能被删除');
					}else{
						$num = $setdeal->delete($value);
					}
				}
			}
			if($num>0){
				$this->success('删除份量单位成功', __APP__.'/SetRecipeInfo/index');
			}else{
				$this->error('删除份量单位失败');
			}
		}else{
			$this->error('请先选择份量单位');
		}
	}

}